﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;

namespace WEI_Share.Helpers
{
    public sealed class Cryptor
    {
        #region Constructor
        /// <summary>
        /// Private constructor
        /// </summary>
        private Cryptor()
        {
        }
        #endregion


        static string passPhrase = ""; // can be any string
        static string saltValue = "";  // can be any string
        static int passwordIterations = 1; // can be any number
        static string initVector = ""; // must be 16 bytes (Characters)



        public static string Encrypt(string input)
        {
            // Test data
            string data = input;
            byte[] utfdata = UTF8Encoding.UTF8.GetBytes(data);
            byte[] saltBytes = UTF8Encoding.UTF8.GetBytes(saltValue);


            // Our symmetric encryption algorithm
            AesManaged aes = new AesManaged();


            // We're using the PBKDF2 standard for password-based key generation
            Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltBytes, passwordIterations);



            // Setting our parameters
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;


            byte[] passwordBytes = password.GetBytes(aes.KeySize / 8);
            byte[] initVectorBytes = UTF8Encoding.UTF8.GetBytes(initVector);


            aes.Key = passwordBytes;
            aes.IV = initVectorBytes;


            // Encryption
            ICryptoTransform encryptTransf = aes.CreateEncryptor();



            // Output stream, can be also a FileStream
            MemoryStream encryptStream = new MemoryStream();
            CryptoStream encryptor = new CryptoStream(encryptStream, encryptTransf, CryptoStreamMode.Write);


            encryptor.Write(utfdata, 0, utfdata.Length);
            encryptor.Flush();
            encryptor.Close();


            // Showing our encrypted content
            byte[] encryptBytes = encryptStream.ToArray();

            string encryptedString = Convert.ToBase64String(encryptBytes);

            return encryptedString;
        }





        public static string Decrypt(string base64Input)
        {
            //byte[] encryptBytes = UTF8Encoding.UTF8.GetBytes(input);
            byte[] encryptBytes = Convert.FromBase64String(base64Input);
            byte[] saltBytes = Encoding.UTF8.GetBytes(saltValue);


            // Our symmetric encryption algorithm
            AesManaged aes = new AesManaged();


            // We're using the PBKDF2 standard for password-based key generation
            Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltBytes, passwordIterations);


            // Setting our parameters
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;


            byte[] passwordBytes = password.GetBytes(aes.KeySize / 8);
            byte[] initVectorBytes = UTF8Encoding.UTF8.GetBytes(initVector);


            aes.Key = passwordBytes;
            aes.IV = initVectorBytes;


            // Now, decryption
            ICryptoTransform decryptTrans = aes.CreateDecryptor();


            // Output stream, can be also a FileStream
            MemoryStream decryptStream = new MemoryStream();
            CryptoStream decryptor = new CryptoStream(decryptStream, decryptTrans, CryptoStreamMode.Write);


            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Flush();
            decryptor.Close();


            // Showing our decrypted content
            byte[] decryptBytes = decryptStream.ToArray();
            string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);


            return decryptedString;
        }



    }
}
